﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using LGE;

namespace MagicFight
{
    public class PlayScreen : GameScreen
    {
        Cursor mouseCursor;
        public EffectsManager effectsManager;
        public List<Magic> magics;
        public List<Character> characters;
        public List<GameObject> gameObjects;
        public List<Vector2> initialPosition;
        public int nPlayers = 4;

        public Vector2 mapBound0 = new Vector2(100, 30);
        Vector2 mapBound1 = new Vector2(1180, 620);

        #region UI
        Texture2D blackScreen;
        Bar bar;
        Texture2D logoProtection;
        Texture2D logoHoly;
        Texture2D logoArcane;
        Texture2D logoElemental;
        List<Vector2> barPosition;
        List<Vector2> pointsPosition;
        #endregion

        public PlayScreen(List<Character> characters)
            : base()
        {
            this.characters = characters;
        }

        public override void LoadContent()
        {
            //System.Threading.Thread.Sleep(2000); // FOR TESTS (thread)
            Magic.ps = this;
            GameObject.ps = this;
            magics = new List<Magic>();
            gameObjects = new List<GameObject>();
            effectsManager = new EffectsManager();
            blackScreen = Engine.Load<Texture2D>("Images\\BlackScreen");
            bar = new Bar(Engine.Load<Texture2D>("Images\\bar"));
            logoProtection = Engine.Load<Texture2D>("Images\\Logo\\protection");
            logoHoly = Engine.Load<Texture2D>("Images\\Logo\\holy");
            logoArcane = Engine.Load<Texture2D>("Images\\Logo\\arcane");
            logoElemental = Engine.Load<Texture2D>("Images\\Logo\\elemental");
            mouseCursor = new Cursor();
            initialPosition = new List<Vector2>() { new Vector2(340, 260), new Vector2(940, 260), new Vector2(340, 460), new Vector2(940, 460) };
            barPosition = new List<Vector2>() { new Vector2(25, 140), new Vector2(1280 - 75, 140), new Vector2(25, 720 - 140), new Vector2(1280 - 75, 720 - 140) };
            pointsPosition = new List<Vector2>() { new Vector2(9, 265), new Vector2(1280 - 34, 265), new Vector2(9, 720 - 290), new Vector2(1280 - 34, 720 - 290) };

            foreach (Character c in characters)
            {
                c.position = initialPosition[c.playerIndex];
                gameObjects.Add(c);
            }
        }

        public override void Update(GameTime gameTime)
        {
            UpdateInput();
            mouseCursor.Update(gameTime);

            // Update GameObjects
            for (int i = 0; i < gameObjects.Count; ++i)
            {
                if (!gameObjects[i].alive) gameObjects.RemoveAt(i);
                else
                {
                    gameObjects[i].Update(gameTime);
                    MapCollision(gameObjects[i]);
                }
            }

            // Update Magics
            for (int i = 0; i < magics.Count; ++i)
            {
                if (magics[i].alive) magics[i].Update(gameTime);
                else magics.RemoveAt(i);
            }
            effectsManager.Update(gameTime);
        }

        private void MapCollision(GameObject obj)
        {
            Vector2 pos = obj.position;
            if (pos.X < mapBound0.X) obj.MapCollision(Direction.Left);
            if (pos.Y < mapBound0.Y) obj.MapCollision(Direction.Up);
            if (pos.X > mapBound1.X) obj.MapCollision(Direction.Right);
            if (pos.Y > mapBound1.Y) obj.MapCollision(Direction.Down);
        }

        private void UpdateInput()
        {
            // Mouse + Keyboard (for tests)
            // P1 movement by keyboard
            /*Vector2 dir = Vector2.Zero;
            if (Input.KeyDown(Keys.W)) dir += Direction.Up;
            if (Input.KeyDown(Keys.A)) dir += Direction.Left;
            if (GameScreenManager.KeyDown(Keys.D)) dir += Direction.Right;
            if (GameScreenManager.KeyDown(Keys.S)) dir += Direction.Down;
            if (dir != Vector2.Zero)
            {
                dir.Normalize();
                characters[0].Move(dir);
            }
            if (GameScreenManager.MouseRClick) Cast(characters[0], 0);
            if (GameScreenManager.KeyPressed(Keys.D1)) 
                Cast(characters[0], 0);
            if (GameScreenManager.KeyPressed(Keys.D2)) Cast(characters[0], 1);
            if (GameScreenManager.KeyPressed(Keys.D3)) Cast(characters[0], 2);
            if (GameScreenManager.KeyPressed(Keys.D4)) Cast(characters[0], 3);
            if (GameScreenManager.KeyPressed(Keys.Q)) Cast(characters[1], 1);
            if (GameScreenManager.KeyPressed(Keys.E)) Cast(characters[1], 2);
            if (GameScreenManager.KeyPressed(Keys.C)) Cast(characters[1], 3);*/
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(blackScreen, mapBound0, new Rectangle((int)mapBound0.X, (int)mapBound0.Y, (int)(mapBound1.X - mapBound0.X), (int)(mapBound1.Y - mapBound0.Y)), Color.White);
            foreach (GameObject obj in gameObjects) obj.Draw(spriteBatch);
            effectsManager.Draw(gameTime, spriteBatch);
            mouseCursor.Draw(spriteBatch);
            
            #region Draw UI
            
            Vector2 dif = new Vector2(50, 0);
            foreach (Character c in characters)
            {
                // health
                bar.Draw(spriteBatch, (float)c.HP / c.TotalHP, barPosition[c.playerIndex], Color.Red, .8f, true);

                // mana
                bar.Draw(spriteBatch, (float)c.MP / c.TotalMP, barPosition[c.playerIndex] + dif, Color.Blue, .8f, true);

                // cast 
                if (c.isCasting)
                    bar.Draw(spriteBatch, (float)c.castTimer / c.castTime, c.position + new Vector2(0, 40), Color.White, 0.2f, false);

                // points
                Vector2 pos = pointsPosition[c.playerIndex];
                Vector2 add = new Vector2(logoProtection.Width + 3);
                if (c.playerIndex > 1) add.Y *= -1;
                if (c.playerIndex == 1 || c.playerIndex == 3) add.X *= -1;

                for (int i = 0; i < c.points[(int)MagicType.Protection]; ++i) { spriteBatch.Draw(logoProtection, pos, Color.White); pos.X += add.X; }
                if (c.points[(int)MagicType.Protection] > 0) { pos.Y += add.Y; pos.X = pointsPosition[c.playerIndex].X; }

                for (int i = 0; i < c.points[(int)MagicType.Holy]; ++i) { spriteBatch.Draw(logoHoly, pos, Color.White); pos.X += add.X; }
                if (c.points[(int)MagicType.Holy] > 0) { pos.Y += add.Y; pos.X = pointsPosition[c.playerIndex].X; }

                for (int i = 0; i < c.points[(int)MagicType.Arcane]; ++i) { spriteBatch.Draw(logoArcane, pos, Color.White); pos.X += add.X; }
                if (c.points[(int)MagicType.Arcane] > 0) { pos.Y += add.Y; pos.X = pointsPosition[c.playerIndex].X; }

                for (int i = 0; i < c.points[(int)MagicType.Elemental]; ++i) { spriteBatch.Draw(logoElemental, pos, Color.White); pos.X += add.X; }
            }
            
            
            
            // Log
            spriteBatch.DrawString(MagicFight.Font, Log.ToString(), new Vector2(400, 500), Color.White);

            #endregion
        }
    }
}   
